package com.bestcem.xm.award.service.impl;

import cn.hutool.core.lang.Validator;
import com.bestcem.xm.award.constant.Constants;
import com.bestcem.xm.award.constant.RequestAwardConstants;
import com.bestcem.xm.award.entity.mongo.BaseEntity;
import com.bestcem.xm.award.entity.pojo.AwardAwardDO;
import com.bestcem.xm.award.entity.pojo.AwardRequestDO;
import com.bestcem.xm.award.enums.AwardRequestStatusEnum;
import com.bestcem.xm.award.enums.AwardTypeEnum;
import com.bestcem.xm.award.service.bo.award.AwardAwardBO;
import com.bestcem.xm.award.service.dto.requestaward.RemoteAwardDTO;
import com.bestcem.xm.award.service.dto.requestaward.RequestAwardDTO;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Optional;

import static com.bestcem.xm.award.enums.AwardRequestStatusEnum.PASS;
import static com.bestcem.xm.award.enums.AwardTypeEnum.REMOTE;

/**
 * 第三方奖励实现类
 *
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/2 14:25
 */
@Slf4j
@Service
public class RemoteAwardServiceImpl extends AbstractAwardAwardService {

    @Override
    public AwardTypeEnum getAwardTypeEnum() {
        return REMOTE;
    }

    @Override
    public ServiceResult<String> doRequestAward(RequestAwardDTO requestAwardDTO) {
        // 返回结构体
        RemoteAwardDTO remoteAwardDTO = new RemoteAwardDTO();
        remoteAwardDTO.setTType(REMOTE.getIndex());
        // 校验
        AwardRequestStatusEnum checkAccount = awardCheck.getCheckAccountResult(requestAwardDTO);
        if (checkAccount.getIndex() != PASS.getIndex()) {
            return super.checkFail(requestAwardDTO.getAwardRequestDO(), checkAccount);
        }
        AwardRequestStatusEnum checkIp = awardCheck.getCheckIpResult(requestAwardDTO);
        if (checkIp.getIndex() != PASS.getIndex()) {
            return super.checkFail(requestAwardDTO.getAwardRequestDO(), checkIp);
        }
        AwardRequestStatusEnum checkCostTime = awardCheck.getCheckCostTimeResult(requestAwardDTO);
        if (checkCostTime.getIndex() != PASS.getIndex()) {
            return super.checkFail(requestAwardDTO.getAwardRequestDO(), checkCostTime);
        }
        AwardRequestStatusEnum checkDevice = awardCheck.getCheckDeviceResult(requestAwardDTO);
        if (checkDevice.getIndex() != PASS.getIndex()) {
            return super.checkFail(requestAwardDTO.getAwardRequestDO(), checkDevice);
        }
        // 奖励id
        String awardId = Optional.ofNullable(requestAwardDTO).map(RequestAwardDTO::getAwardAwardDO)
                .map(BaseEntity::getId).orElse(Constants.BLANK);
        if (StringUtils.isBlank(awardId)) {
            return ServiceResult.fail(RequestAwardConstants.MISSING_AWARD_ID);
        }
        // 奖励信息 返回信息只需要page.url
        AwardAwardDO awardAwardDO = requestAwardDTO.getAwardAwardDO();
        remoteAwardDTO.setPage(Optional.ofNullable(awardAwardDO).map(AwardAwardDO::getPage).orElse(null));
        // 待插入AWARD_REQUEST数据
        AwardRequestDO request = Optional.ofNullable(requestAwardDTO).map(RequestAwardDTO::getAwardRequestDO).orElse(null);
        if (Validator.isNull(request)) {
            return ServiceResult.fail(RequestAwardConstants.MISSING_AWARD_REQUEST);
        }
        request.setStatus(PASS.getIndex());
        // 插入AWARD_REQUEST数据
        awardRequestDao.insert(request);
        String data = JsonUtil.transferToJsonSnake(remoteAwardDTO);
        return ServiceResult.success(data);
    }

    /**
     * 业务检查奖励数据
     *
     * @param awardAward 奖励数据
     * @return
     */
    @Override
    public ServiceResult<String> independentCheckAward(AwardAwardBO awardAward) {
        Optional<AwardAwardBO> optional = Optional.ofNullable(awardAward);
        if (!optional.map(AwardAwardBO::getPage).map(AwardAwardBO.Page::getUrl).isPresent()) {
            return ServiceResult.fail("奖励跳转链接为空！");
        } else {
            if (optional.map(AwardAwardBO::getPage).map(AwardAwardBO.Page::getUrl).orElse("").length() > Constants.REMOTE_AWARD_URL_MAX_LENGTH) {
                return ServiceResult.fail("奖励跳转链接url长度超过限制!");
            }
        }
        if (!optional.map(AwardAwardBO::getBasic).map(AwardAwardBO.Basic::getStartTime).isPresent() || awardAward.getBasic().getStartTime().getTime() < DateUtil.now().getTime()) {
            awardAward.getBasic().setStartTime(DateUtil.getDate());
        }
        if (!optional.map(AwardAwardBO::getBasic).map(AwardAwardBO.Basic::getStopTime).isPresent()) {
            return ServiceResult.fail("奖励结束时间缺失！");
        }
        if (awardAward.getBasic().getStopTime().compareTo(awardAward.getBasic().getStartTime()) < 0) {
            return ServiceResult.fail("结束时间不能在开始时间或当前时间之前!");
        }
        awardAward.getPage().setPc(null);
        awardAward.getPage().setMobile(null);
        awardAward.setIssue(null);
        awardAward.setAwards(null);
        return ServiceResult.success();
    }
}
